﻿
#include "pch.h"
#include "BasicExcel.h"


using namespace YExcel;


#include <windows.h>

uint16_t strtmp[128] = { 0 };

void ExcelShowAll(BasicExcelWorksheet* sheet1);


/*
 * 返回值：数组，最多10 组
 */

#define PREC_MATCH			(1 << 0)
#define VALUE_MATCH			(1 << 2)
#define FOOT_MATCH			(1 << 3)


#define SEARCH_RET_MAX		10


uint32_t ret_search_flag[SEARCH_RET_MAX] = { 0 };
wchar_t ret_search_name[SEARCH_RET_MAX][256] = { 0 };
wchar_t ret_search_id[SEARCH_RET_MAX][256] = { 0 };

uint8_t ret_search_cnt = 0;


int ExcelSearch(FILE * f, BasicExcelWorksheet* sheet1, const wchar_t* foot, const wchar_t* value) {

	wchar_t prefix_temp[128] = { 0 };

#if 1
	/* 拼装 \ value 字符串 */
	wchar_t value_temp[128] = { 0 };

	if (value)
	{
		wcscpy(value_temp, L"\\");
		wcscat(value_temp, value);

		// 规则，如果 value 里带有 空格，则只取空格前面的值
		wchar_t* p = wcschr(value_temp, L' ');
		if (p) {
			*p = '\0';

			wcscpy(prefix_temp, p + 1);
		}

		value = value_temp;
	}
#endif

#if 1
	if (foot)
	{
		if (wcscmp(foot, L"C0805") == 0) {
			foot++;
		}
		if (wcscmp(foot, L"R0805") == 0) {
			foot++;
		}
		if (wcscmp(foot, L"C0603") == 0) {
			foot++;
		}
		if (wcscmp(foot, L"R0603") == 0) {
			foot++;
		}
	}
#endif

	size_t maxRows = sheet1->GetTotalRows();
	size_t maxCols = sheet1->GetTotalCols();

	ret_search_cnt = 0;
	uint32_t match_prec = 0;

	for (size_t r = 0; r < maxRows; ++r)
	{
		BasicExcelCell* cell = sheet1->Cell(r, 1);

		if (cell->Type() == BasicExcelCell::WSTRING) {

			match_prec = (wcsstr(cell->GetWString(), L"1%")) ? PREC_MATCH : 0;

			/* 开始比对 */
			if (foot && value) {
				if ((wcsstr(cell->GetWString(), foot)) && \
					(wcsstr(cell->GetWString(), value)))
				{
					if (prefix_temp[0]) {
						if (wcsstr(cell->GetWString(), prefix_temp)) {
							fwprintf(f, L"[prefix]");
						}
					}
					fwprintf(f, L"\t\t获取到品名: %s, 品号: %s\r\n", cell->GetWString(), sheet1->Cell(r, 0)->GetWString());
					ret_search_flag[ret_search_cnt] = VALUE_MATCH | FOOT_MATCH | match_prec;
					wcscpy(ret_search_name[ret_search_cnt], cell->GetWString());
					wcscpy(ret_search_id[ret_search_cnt], sheet1->Cell(r, 0)->GetWString());
					if (ret_search_cnt < SEARCH_RET_MAX) {
						ret_search_cnt ++;
					}
				}
			}
			else if (foot) {
				if (wcsstr(cell->GetWString(), foot))
				{
					fwprintf(f, L"\t\t[F]获取到品名: %s, 品号: %s\r\n", cell->GetWString(), sheet1->Cell(r, 0)->GetWString());
					ret_search_flag[ret_search_cnt] = FOOT_MATCH | match_prec;
					wcscpy(ret_search_name[ret_search_cnt], cell->GetWString());
					wcscpy(ret_search_id[ret_search_cnt], sheet1->Cell(r, 0)->GetWString());
					if (ret_search_cnt < SEARCH_RET_MAX) {
						ret_search_cnt ++;
					}
				}
			}
			else if (value) {
				if (wcsstr(cell->GetWString(), value))
				{
					fwprintf(f, L"\t\t[V]获取到品名: %s, 品号: %s\r\n", cell->GetWString(), sheet1->Cell(r, 0)->GetWString());
					ret_search_flag[ret_search_cnt] = VALUE_MATCH | match_prec;
					wcscpy(ret_search_name[ret_search_cnt], cell->GetWString());
					wcscpy(ret_search_id[ret_search_cnt], sheet1->Cell(r, 0)->GetWString());
					if (ret_search_cnt < SEARCH_RET_MAX) {
						ret_search_cnt ++;
					}
				}
			}
		}
	}

	return ret_search_cnt;
}


uint8_t get_best_match(void)
{
	uint8_t match_id =  0;
	uint32_t match_flag = 0;

	for (uint8_t i = 0; i < ret_search_cnt; i++) {
		if (ret_search_flag[i] > match_flag) {
			match_flag = ret_search_flag[i];
			match_id = i;
		}
	}

	return match_id;
}



void ExcelShowAll(FILE * f, BasicExcelWorksheet* sheet1) {

	if (sheet1 == NULL) {
		return;
	}

	size_t maxRows = sheet1->GetTotalRows();
	size_t maxCols = sheet1->GetTotalCols();
	//cout << "Dimension of " << sheet1->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;
	cout << "Dimension of " << " (" << maxRows << ", " << maxCols << ")" << endl;

	fprintf(f, "          ");
	for (size_t c = 0; c < maxCols; ++c) printf("%10lld", c + 1);
	cout << endl;

	for (size_t r = 0; r < maxRows; ++r)
	{
		fprintf(f, "%10lld\t", r + 1);
		for (size_t c = 0; c < maxCols; ++c)
		{
			BasicExcelCell* cell = sheet1->Cell(r, c);
			switch (cell->Type())
			{
			case BasicExcelCell::UNDEFINED:
				fprintf(f, "          ");
				break;

			case BasicExcelCell::INT:
				fprintf(f, "%10d", cell->GetInteger());
				break;

			case BasicExcelCell::DOUBLE:
				fprintf(f, "%10.6lf", cell->GetDouble());
				break;

			case BasicExcelCell::STRING:
				fprintf(f, "%10s", cell->GetString());
				//strcpy(strtmp, cell->GetString());
				break;

			case BasicExcelCell::WSTRING:
				fwprintf(f, L"%10s", cell->GetWString());
#if 0
				strcpy((char*)strtmp, (const char*)cell->GetWString());
				//fwprintf(L"%s\r\n", L"我的品名");
				//if (cell->GetWString() == L"品号") {
				//if (L"品号" == L"品号") {
				if (wcscmp(cell->GetWString(), L"品号") == 0) {
					fprintf(f, "Name Matched !\r\n");
				}
				else {

				}
#endif
				break;
			}
		}
		cout << endl;
	}
}





int ExcelProgress(FILE * f, wchar_t * pathInput, wchar_t * pathDatabase) {

	int ret;

	BasicExcel e;
	BasicExcel x;

	wchar_t match_list[2048];
	wchar_t value_str[128];
	wchar_t foot_int_tmp[64];

#if 1
	if (!e.Load(pathInput)) {
		fwprintf(f, L"加载 %s 文档失败 \n", pathInput);
		return 3;
	}
#endif

#if 1
	if (!x.Load(pathDatabase)) {
		//if (!e.Load(L"D:\\Documents\\bom.xls")) {
		fwprintf(f, L"加载 %s 文档失败 \n", pathDatabase);
		return 4;
	}
	else {
		fprintf(f, "加载成功！\r\n");
	}
#endif

	setlocale(LC_CTYPE, "");

	fwprintf(f, L"%s", L"开始行动");

	//SetConsoleOutputCP(CP_ACP);//(CP_UTF8);
	//SetConsoleCP(CP_ACP);

	BasicExcelWorksheet* database = x.GetWorksheet(L"101-115 商品信息清单20250710");
	if (database)
	{
		/* 显示全部内容 */
		//ExcelShowAll(database);

		ExcelSearch(f, database, L"0603", L"\\10K");
	}
	else {
		return 5;
	}

	BasicExcelWorksheet* BOM_Input = e.GetWorksheet(L"Sheet1");
	if (BOM_Input) {
		
		size_t maxRows = BOM_Input->GetTotalRows();
		size_t maxCols = BOM_Input->GetTotalCols();

		for (size_t row = 1; row < maxRows; row++) {
			
			BasicExcelCell* cell = BOM_Input->Cell(row, 1);
			const wchar_t * foot;
			switch (cell->Type()) {
				case BasicExcelCell::WSTRING:
					foot = cell->GetWString();
					break;
				case BasicExcelCell::INT:
					wsprintf(foot_int_tmp, L"%04d", cell->GetInteger());
					foot = foot_int_tmp;
					fprintf(f, "fo int");
					break;
				case BasicExcelCell::DOUBLE:
					wsprintf(foot_int_tmp, L"%04d", cell->GetDouble());
					foot = foot_int_tmp;
					fprintf(f, "fo db");
					break;
				default:
					foot = L"封装类型错误";
					fprintf(f, "Type Error!\r\n");
					break;
			}

			BasicExcelCell* cell2 = BOM_Input->Cell(row, 2);
			const wchar_t * value;
			switch (cell->Type()) {
				case BasicExcelCell::WSTRING:
					value = cell2->GetWString();
					break;
				case BasicExcelCell::DOUBLE:/* 603 转换为 '0603' */
					wsprintf(value_str, L"%4.0f", cell2->GetDouble());
					value = value_str;
					break;
				default:
					value = L"值类型错误";
					fprintf(f, "Type Error!\r\n");
					break;
			}

			/* 搜索该值 */
			fwprintf(f, L"查找：【封装】 %s 【值】 %s\r\n", foot, value);
			//ExcelSearch(database, L"0603", L"\\10K");
			ret = ExcelSearch(f, database, foot, value);
			if (ret > 0) {
				/* 获取最佳品名品号并写入 */
				BOM_Input->Cell(row, maxCols)->SetWString(ret_search_id[get_best_match()]);
				BOM_Input->Cell(row, maxCols + 1)->SetWString(ret_search_name[get_best_match()]);

				/* 如果 ret > 1, 写入匹配项 */
				if (ret > 1) {
					wchar_t * s = match_list;
					s += wsprintf(s, L"匹配列表(%d个匹配项目)：", ret);
					for (uint8_t i = 0; i < ret; i ++) {
						s += wsprintf(s, L"\r\n%s:%s", ret_search_id[i], ret_search_name[i]);
					}
					BOM_Input->Cell(row, maxCols + 2)->SetWString(match_list);
				}
			} else {
				/* 写入未找到 */
				BOM_Input->Cell(row, maxCols)->SetWString(L"未找到料号");
			}
		}

		/* 保存结果 */
		if (e.SaveAs("./整合结果.xls")) {
			return 0;	//保存成功
		}
		else {
			return 1;	//保存失败
		}
	}

	return 2;
}




